package fi.joensuu.mpm.geo;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import fi.joensuu.mpm.Utils;
import fi.joensuu.mpm.data.Place;

public class Geoprocessor {
	
	static Random rand = new Random();
	
	/**
	 * return places which are close to current user location
	 * 
	 * @param places
	 * @param numberOfPlaces
	 * @return
	 */
	public static ArrayList<Place> getClosePlaces(Context context, ArrayList<Place> places, int numberOfPlaces) {
		if (places.size() == 0) {
			return places;
		}
		
		ArrayList<Place> _places = places;
		Location currentLocation = getLastLocation(context);
		Place place;
		Location location;
		for (int i = 0; i < places.size(); i++) {
			place = _places.get(i);
			location = new Location(LocationManager.GPS_PROVIDER);
			location.setLatitude(_places.get(i).getLatitude());
			location.setLongitude(_places.get(i).getLongitude());
			place.setDistance(currentLocation.distanceTo(location));
		}
		
		float minDistance;
		float distance;
		int index = 0;
		for (int i = 0; i < places.size(); i++) {
			minDistance = places.get(i).getDistance();
			for (int j = i; j < places.size(); j++) {
				distance = places.get(j).getDistance();
				if (minDistance > distance) {
					minDistance = distance;
					index = j;
				}
			}
			place = places.get(i);
			places.set(i, places.get(index));
			places.set(index, place);
		}
		ArrayList<Place> resultPlaces = new ArrayList<Place>();
		for (int i = 0; i < numberOfPlaces; i++) {
			resultPlaces.add(places.get(i));
		}
		return resultPlaces;
	}
	
	/**
	 * return
	 * 
	 * @param place
	 * @return
	 */
	public static float getDistance(Context context, Place place) {		
		Location currentLocation = getLastLocation(context);
		Location location = new Location(LocationManager.GPS_PROVIDER);
		location.setLatitude(place.getLatitude());
		location.setLongitude(place.getLongitude());
		return currentLocation.distanceTo(location);
	}
	
	private static Location getLastLocation(Context context)
	{
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		String provider = locationManager.getBestProvider(criteria, true);
		locationManager.requestLocationUpdates(provider, 0, 0, new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				
			}
		});
		return locationManager.getLastKnownLocation(provider);
	}
	
}
